package sys.knot.validator;

import sys.knot.configuration.ConfigurationException;
import sys.knot.model.ParameterType;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MaxLength implements Validator {

    private int maxLength;

    @Override
    public void addValidatorParameters(String[] args) throws ConfigurationException {
        this.maxLength = Integer.parseInt(args[0]);
    }

    @Override
    public List<ParameterType> getSupportedTypes() {
        return new ArrayList<ParameterType>() {{
            add(ParameterType.STRING);
        }};
    }

    @Override
    public void validate(String parameterName, String parameterValue) throws ValidationException {
        if (parameterValue.length() > maxLength) {
            throw new ValidationException(parameterName + ": " + parameterValue + ", is too long - maximum: " + maxLength + " chars.");
        }
    }

    @Override
    public void validate(String s, int i) throws ValidationException {
        throw new ValidationException("Validator only applicable to STRING");
    }

    @Override
    public void validate(String s, Date date) throws ValidationException {
        throw new ValidationException("Validator only applicable to STRING");
    }

    @Override
    public void validate(String s, String[] strings) throws ValidationException {
        throw new ValidationException("Validator only applicable to STRING");
    }

    @Override
    public void validate(String s, int[] ints) throws ValidationException {
        throw new ValidationException("Validator only applicable to STRING");
    }

    @Override
    public String toString() {
        return "MaxLength(" + maxLength + ")";
    }
}
